package org.elanfox.intellicommunity.intellicommunityadmin.controller.msg;

import org.elanfox.intellicommunity.biz.dto.condition.AlarmGroupSearchCondition;
import org.elanfox.intellicommunity.biz.dto.msg.AlarmGroupDTO;
import org.elanfox.intellicommunity.biz.dto.msg.AlarmWorkOrderSaveDTO;
import org.elanfox.intellicommunity.biz.entity.msg.AlarmMsgGroupPO;
import org.elanfox.intellicommunity.biz.entity.msg.AlarmMsgOptLogPO;
import org.elanfox.intellicommunity.biz.entity.msg.AlarmMsgPO;
import org.elanfox.intellicommunity.biz.entity.msg.AlarmWorkOrderPO;
import org.elanfox.intellicommunity.biz.service.core.msg.AlarmMsgGroupService;
import org.elanfox.intellicommunity.biz.service.core.msg.AlarmMsgOptLogService;
import org.elanfox.intellicommunity.biz.service.core.msg.AlarmMsgService;
import org.elanfox.intellicommunity.biz.service.core.msg.AlarmWorkOrderService;
import org.elanfox.intellicommunity.common.base.ApiResult;
import org.elanfox.intellicommunity.common.base.ResultCodeEnum;
import org.elanfox.intellicommunity.common.constant.Constant;
import org.elanfox.intellicommunity.common.ienum.MsgEnums;
import org.elanfox.intellicommunity.intellicommunityadmin.vo.AlarmWorkOrderVO;
import org.elanfox.intellicommunity.intellicommunityadmin.vo.OnRoadInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author allan
 * @version 1.0.0
 * @date 2019/12/27
 */
@Controller
@RequestMapping("/msg/alarm")
public class AlarmMsgController {
    @Autowired
    private AlarmMsgGroupService alarmMsgGroupService;
    @Autowired
    private AlarmMsgService alarmMsgService;
    @Autowired
    private AlarmMsgOptLogService alarmMsgOptLogService;
    @Autowired
    private AlarmWorkOrderService alarmWorkOrderService;

    @GetMapping("/list")
    public String list(
            @ModelAttribute("condition") AlarmGroupSearchCondition condition,
            @RequestParam(required = false, defaultValue = "1") int pageIndex,
            Model model
    ) {
        Page<AlarmMsgGroupPO> rechargePOS = alarmMsgGroupService.findAll(pageIndex, Constant.DEFAULT_PAGE_SIZE, condition);
        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("totalRecords", rechargePOS.getTotalElements());
        model.addAttribute("totalPages", rechargePOS.getTotalPages());
        model.addAttribute("list", rechargePOS.getContent());
        model.addAttribute("alarmStatuses", MsgEnums.AlarmStatus.values());

        return "msg/alarm_group_list";
    }

    @GetMapping("/msgList")
    public String msgList(
            @ModelAttribute("groupId") Long groupId,
            @RequestParam(required = false, defaultValue = "1") int pageIndex,
            Model model
    ) {
        Page<AlarmMsgPO> alarmMsgPOS = alarmMsgService.findByGroup(groupId, pageIndex, Constant.DEFAULT_PAGE_SIZE);
        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("totalRecords", alarmMsgPOS.getTotalElements());
        model.addAttribute("totalPages", alarmMsgPOS.getTotalPages());
        model.addAttribute("list", alarmMsgPOS.getContent());

        return "msg/alarm_msg_list";
    }

    @PostMapping("/onRoad")
    @ResponseBody
    public ApiResult onRoad(Long groupId, String policeName, String policeMobile) {
        alarmMsgGroupService.onRoad(groupId, policeName, policeMobile);

        return ApiResult.resultWith(ResultCodeEnum.SUCCESS);
    }

    @PostMapping("/handle")
    @ResponseBody
    public ApiResult handle(Long groupId) {
        alarmMsgGroupService.handle(groupId);

        return ApiResult.resultWith(ResultCodeEnum.SUCCESS);
    }

    @PostMapping("/ignore")
    @ResponseBody
    public ApiResult ignore(Long groupId, String remark) {
        alarmMsgGroupService.ignore(groupId, remark);

        return ApiResult.resultWith(ResultCodeEnum.SUCCESS);
    }

    @PostMapping("/submit")
    @ResponseBody
    public ApiResult submit(Long groupId, AlarmWorkOrderSaveDTO orderSaveDTO) {
        alarmMsgGroupService.submit(groupId, orderSaveDTO);

        return ApiResult.resultWith(ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/policeInfo")
    @ResponseBody
    public ApiResult policeInfo(Long groupId) {
        AlarmMsgOptLogPO alarmMsgOptLogPO = alarmMsgOptLogService.findByGroupAndStatus(groupId, MsgEnums.AlarmStatus.ON_LOAD);
        OnRoadInfoVO onRoadInfoVO = new OnRoadInfoVO(alarmMsgOptLogPO.getPoliceName(), alarmMsgOptLogPO.getPoliceMobile());

        return ApiResult.resultWith(ResultCodeEnum.SUCCESS, onRoadInfoVO);
    }

    @GetMapping("/workOrder")
    @ResponseBody
    public ApiResult workOrder(Long groupId) {
        AlarmWorkOrderPO alarmWorkOrderPO = alarmWorkOrderService.findByGroup(groupId);

        return ApiResult.resultWith(ResultCodeEnum.SUCCESS, new AlarmWorkOrderVO(alarmWorkOrderPO));
    }

    /**
     * 操作日志
     *
     * @param groupId
     * @param model
     * @return
     */
    @GetMapping("/optList")
    public String optList(Long groupId, Model model) {
        List<AlarmMsgOptLogPO> alarmMsgOptLogPOS = alarmMsgOptLogService.findByGroup(groupId);

        model.addAttribute("list", alarmMsgOptLogPOS);

        return "msg/alarm_opt_list";
    }

    @GetMapping("/forDesktop")
    public String forDesktop(
            @ModelAttribute("condition") AlarmGroupSearchCondition condition,
            @RequestParam(required = false, defaultValue = "1") int pageIndex,
            Model model
    ) {
        Page<AlarmMsgGroupPO> rechargePOS = alarmMsgGroupService.findAll(pageIndex, Constant.DEFAULT_PAGE_SIZE, condition);

        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("totalRecords", rechargePOS.getTotalElements());
        model.addAttribute("totalPages", rechargePOS.getTotalPages());
        model.addAttribute("list", rechargePOS.getContent());
        model.addAttribute("alarmStatuses", MsgEnums.AlarmStatus.values());


        return "msg/alarm_for_desktop";
    }

    @PostMapping("/alarmingList")
    @ResponseBody
    public ApiResult alarmingList() {
        List<AlarmMsgGroupPO> alarmMsgGroupPOS = alarmMsgGroupService.findLastInAlarm();
        List<AlarmGroupDTO> alarmGroupDTOS = new ArrayList<>();
        alarmMsgGroupPOS.forEach(p -> alarmGroupDTOS.add(new AlarmGroupDTO(p)));

        return ApiResult.resultWith(ResultCodeEnum.SUCCESS, alarmGroupDTOS);
    }
}
